Attribute VB_Name = "modIRCFunc"
Option Explicit

' This file is part of Nightowl.
'
' Nightowl is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.

' Nightowl is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.

' You should have received a copy of the GNU General Public License
' along with Nightowl.  If not, see <http://www.gnu.org/licenses/>.


Const RootDate = #1/1/1970#

Function TrimNonAlpha(TextString As String)
' This function strips off any leading and trailing
' characters that are not alphanumeric. (A-Z,a-z,1-0)

' This function is most useful for stripping the
' user mode identification character off a person's
' nickname, for example, the @ symbol that prefixes
' channel operators.

Dim firstChar As String

If Len(TextString) = 0 Then TrimNonAlpha = "": Exit Function

firstChar = Left(TextString, 1)

' Phase 1 check
If Asc(firstChar) < 48 Or Asc(firstChar) > 57 Then
  ' Phase 1 fail
  GoTo Phase2
Else
  ' Phase 1 pass
  TrimNonAlpha = TextString
  Exit Function
End If

Phase2:
' Phase 2 check
If Asc(firstChar) < 65 Or Asc(firstChar) > 90 Then
  ' Phase 2 fail
  GoTo Phase3
Else
  ' Phase 2 pass
  TrimNonAlpha = TextString
  Exit Function
End If

Phase3:
' Phase 3 check
If Asc(firstChar) < 97 Or Asc(firstChar) > 122 Then
  ' Phase 3 fail
  GoTo TrimFirst
Else
  ' Phase 3 pass
  TrimNonAlpha = TextString
  Exit Function
End If

TrimFirst:
TrimNonAlpha = Right(TextString, Len(TextString) - 1)

End Function

Function GetNick(mask As String)
' This function retrieves the nick from a nick!hostmask
' string.

' Example:
' Mask: user!ident@host.com
' Result: user

Dim FLoc As String

' Find the first !
FLoc = InStr(mask, "!")
If FLoc = 0 Then
  'MsgBox "Error in GetNick: not a user/host mask." + vbCrLf + vbCrLf + "Mask: " + Mask, vbOKOnly + vbCritical, "Error"
  GetNick = mask
  Exit Function
End If
GetNick = Left(mask, (FLoc - 1))

End Function

Function GetIdent(mask As String)
' This function retrieves the ident from a nick!hostmask
' string.

' Example:
' Mask: user!ident@host.com
' Result: ident

Dim FLoc As String, SLoc As String, Tloc As String



' Find the ! and @
FLoc = InStr(mask, "!")
SLoc = InStr(mask, "@")
If FLoc = 0 Or SLoc = 0 Then
  'MsgBox "Error in GetIdent: not a user/host mask." + vbCrLf + vbCrLf + "Mask: " + Mask, vbOKOnly + vbCritical, "Error"
  GetIdent = mask
  Exit Function
End If
Tloc = SLoc - FLoc - 1
GetIdent = Mid(mask, FLoc + 1, Tloc)

End Function

Function GetHost(mask As String)
' This function retrieves the hostname from a nick!hostmask
' string.

' Example:
' Mask: user!ident@host.com
' Result: host.com

Dim FLoc As String

' Find the @
FLoc = InStr(mask, "@")
If FLoc = 0 Then
  'MsgBox "Error in GetHost: not a user/host mask." + vbCrLf + vbCrLf + "Mask: " + Mask, vbOKOnly + vbCritical, "Error"
  GetHost = mask
  Exit Function
End If
GetHost = Mid(mask, (FLoc + 1), (Len(mask) - FLoc))

End Function

Function GetBareNick(ByVal FullNick As String)
' This is a legacy holdover for an old function.

GetBareNick = TrimNonAlpha(FullNick)

End Function

Function StripCodes(ByVal Data As String)

' function v2.0
' This function attempts to strip control codes
' out of an IRC string. Control codes include
' color control and bold/underline control

Dim result As String
Dim vpos As Long


For vpos = 1 To Len(Data)
  
  ' If we are over 127, definitiely a control char, skip.
  'If Asc(Mid(Data, vpos, 1)) > 127 Then GoTo GoNext
  
  ' If we Equal 3, go to the special case handler.
  If Asc(Mid(Data, vpos, 1)) = 3 Then GoTo ColorHandle
  
  ' Ok, Now, if we're less than 31, definitely
  ' a contrl char.
  If Asc(Mid(Data, vpos, 1)) < 32 Then GoTo GoNext
  
  ' It seems all worked out. So we'll add the
  ' char to the result.
  result = result + Mid(Data, vpos, 1)

  ' Jump to the NEXT statement.
  GoTo GoNext
  
ColorHandle:
  ' This is our special case handler. We're going
  ' to keep increasing vpos until we find something
  ' that is NOT a number, a comma, or another
  ' control char.
  
  ' We have to check the NEXT char.
  vpos = vpos + 1
  
  ' Are we OVER the limit??
  If vpos > Len(Data) Then
    vpos = vpos - 1
    GoTo GoNext
  End If
  
  ' Now, we're going to check a few things:
  
  ' Is this char a control char?
  If Asc(Mid(Data, vpos, 1)) < 31 Then GoTo ColorHandle
  If Asc(Mid(Data, vpos, 1)) > 127 Then GoTo ColorHandle
  
  ' Is it a NUMBER?
  If Asc(Mid(Data, vpos, 1)) > 47 And Asc(Mid(Data, vpos, 1)) < 58 Then GoTo ColorHandle
      
  ' Is it a COMMA?
  If Asc(Mid(Data, vpos, 1)) = 44 Then GoTo ColorHandle
      
  ' It looks like the next char is OK.
  ' So we minus one off of vpos and allow the loop to
  ' conclude.
      vpos = vpos - 1

GoNext:
Next

' Ahhh, now that we're out of that loop, we'll
' finish up and return!
result = Trim(result)
StripCodes = result
End Function

Function StripFirst(ByVal InString As String)
Dim OutString As String

Select Case Left(InString, 1)
  Case "/", ":", Chr(1)
  OutString = Right(InString, Len(InString) - 1)
  Case Else
  OutString = InString
End Select

StripFirst = OutString

End Function

Function GetVBDate(ByVal IRCTime As Long) As Date
Dim iTime As Date

iTime = IRCTime
iTime = iTime / 86400
iTime = (RootDate + iTime) - (18000 / 86400)
GetVBDate = iTime

End Function

Function GetIRCDate(ByVal VBDate As Date) As Long
Dim iTime As Date

iTime = VBDate
iTime = iTime - RootDate
iTime = (iTime * 86400) + 18000
GetIRCDate = iTime
End Function

Function GetTimeString(ByVal NumSecs As Long)

Dim FinalString As String
Dim wks As Integer, days As Integer, hrs As Integer, mins As Integer, secs As Integer





If NumSecs < 0 Then NumSecs = 0

NumSecs = Int(NumSecs)

' figure out how many weeks
wks = Int(NumSecs / 604800)
If wks <> 0 Then
  FinalString = Trim(Str(wks)) + "wks "
End If

NumSecs = NumSecs - (wks * 604800)

' figure out how many days
days = Int(NumSecs / 86400)
If days <> 0 Then
  FinalString = FinalString + Trim(Str(days)) + "days "
End If

NumSecs = NumSecs - (days * 86400)

' Figure out how many hours
hrs = Int(NumSecs / 3600)
If hrs <> 0 Then
  FinalString = FinalString + Trim(Str(hrs)) + "hrs "
End If

NumSecs = NumSecs - (hrs * 3600)

' figure out how many minutes
mins = Int(NumSecs / 60)
If mins <> 0 Then
  FinalString = FinalString + Trim(Str(mins)) + "mins "
End If

NumSecs = NumSecs - (mins * 60)

' figure out how many seconds
secs = NumSecs
If secs <> 0 Then
  FinalString = FinalString + Trim(Str(secs)) + "secs"
End If

If FinalString = "" Then FinalString = "0secs"

GetTimeString = FinalString

End Function

